/*
 * loadCatalog.java
 *
 * Created on March 26, 2007, 7:11 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package edu.byu.isys413.actions;

import javax.servlet.http.*;
import edu.byu.isys413.group1E.data.*;
import edu.byu.isys413.group1E.handlers.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.text.NumberFormat;

/*
 * Responds to a call from the gui to process a request to load all available 
 * rentals.  Clears the transaction at the end
 *
 * Created on March 26, 2007, 7:11 AM
 *
 * @author Jon Tehero
 *
 */
public class loadRentals implements edu.byu.isys413.web.Action {
    
    /** Creates a new instance of loadCatalog */
    public loadRentals() {
    }
    
    /**
     * Loads in the catalog of available rental items.  It creates an ArrayList of ForRentBO's
     * which the rentalCatalog.jsp can display to the user.  Also, at this point we know the user is going to make a "rental" transaction, so
     * a new transaction object is created and saved over the old one.  In the GUI, a warning displays before this action is performed.
     * @param request ...
     * @param response ...
     * @return String the name of the jsp to call
     * @throws java.lang.Exception ...
     */
    public String process(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        RntlHandler rHandler = RntlHandler.getInstance();
        TransactionDAO TXDAO = TransactionDAO.getInstance();
        UpdateController updateController = UpdateController.getInstance();
        
        ArrayList<ForRentBO> rentalCatalog = rHandler.getAllAvailableRentalItems();
        
        //this is the precident for clearing the transaction
        TransactionBO TXBO = TXDAO.create();
        CustomerBO customer = (CustomerBO)session.getAttribute("customer");
        TXBO.setCustomerID(customer.getCustID());
        TXBO.setStoreID("0000011106a3cc370000007f000002");
        TXBO.setEmployeeID("0000011106a3cc390000007f000002");
        //set the date for the transaction
        Calendar cal = Calendar.getInstance();
        Date today = cal.getTime();
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        String todayDate = fmt.format(today);
        TXBO.setTransDate(todayDate);
        
        session.setAttribute("TX", TXBO);
        
        request.setAttribute("rentalCatalog", rentalCatalog);
        
        return "rentalCatalog.jsp";
    }//process
    
    
}//loadCatalog class
